/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package samoloty.view;

import Utils.MainValidator;
import Utils.ScreensOrder;
import Utils.TextFieldFilter;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.BorderFactory;
import javax.swing.InputMap;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.event.ChangeListener;
import javax.swing.text.Caret;
import javax.swing.text.JTextComponent;
import javax.swing.text.PlainDocument;

/**
 *
 * @author borysfan
 */
public class MyTextField extends JTextField {

    private MyTextField instance;

    public MyTextField() {
        super();
        initTextField();
        instance = this;
    }

    public MyTextField(int x) {
        super(x);
        initTextField();
        instance = this;
    }

    private void initActions() {
        addFocusListener(new TextFieldFocus());
        addKeyListener(new KeyBoardListener());
    }

    private void initTextField() {
        setFont(new Font("Tahome", Font.BOLD, 16));
        setHorizontalAlignment(JTextField.CENTER);
        setBackground(MyColors.getBlack());
        setForeground(MyColors.getWhite());
        MyCaret c = new MyCaret();
        c.setBlinkRate(300);
        setCaret(c);
        initActions();
        InputMap inputMap = getInputMap();
        ActionMap actionMap = getActionMap();
        KeyStroke exitKey = KeyStroke.getKeyStroke(KeyEvent.VK_E, KeyEvent.CTRL_MASK);
        KeyStroke exitKey2 = KeyStroke.getKeyStroke("ESCAPE");
        KeyStroke resetKey = KeyStroke.getKeyStroke(KeyEvent.VK_R, KeyEvent.CTRL_MASK);
        KeyStroke helpKey = KeyStroke.getKeyStroke(KeyEvent.VK_H, KeyEvent.CTRL_MASK);
        inputMap.put(resetKey, "resetK");
        inputMap.put(exitKey, "exitK");
        inputMap.put(exitKey2, "exitK");
        inputMap.put(helpKey, "helpK");
        Action resetAA = new AbstractAction() {

            public void actionPerformed(ActionEvent e) {
                ScreensOrder order = ScreensOrder.getInstance();
                order.setResetPanel();
            }
        };
        Action exitAA = new AbstractAction() {

            public void actionPerformed(ActionEvent e) {
                ScreensOrder order = ScreensOrder.getInstance();
                order.setExitPanel();
            }
        };
        Action helpAA = new AbstractAction() {

            public void actionPerformed(ActionEvent e) {
            }
        };
        actionMap.put("resetK", resetAA);
        actionMap.put("exitK", exitAA);
        actionMap.put("helpK", helpAA);
    }

    public void setPosition(Point p) {
        this.setBounds(p.x, p.y, (int) getPreferredSize().getWidth(), (int) getPreferredSize().getHeight());

    }

    public void setFieldFilter(String s) {
        setDocument(new TextFieldFilter(s));
    }

    public void setFieldFilter(String s, int limit) {
        setDocument(new TextFieldFilter(s, limit));
    }

    public void setFieldFilter(PlainDocument pd) {
        setDocument(pd);
    }

    public void moveLeft(int x) {
        this.setLocation(new Point(this.getLocation().x - x, this.getLocation().y));
    }

    public void moveRight(int x) {
        this.setLocation(new Point(this.getLocation().x + x, this.getLocation().y));
    }

    private class TextFieldFocus implements FocusListener {

        public void focusGained(FocusEvent e) {
            instance.setBorder(BorderFactory.createLineBorder(Color.WHITE, 3));
            instance.selectAll();
        }

        public void focusLost(FocusEvent e) {
            instance.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1));
        }
    }

    private class KeyBoardListener implements KeyListener {

        public void keyTyped(KeyEvent e) {
        }

        public void keyPressed(KeyEvent e) {
            int code = e.getKeyCode();

            if (code == KeyEvent.VK_F1) {
                ScreensOrder order = ScreensOrder.getInstance();
                order.prevScreen();
            } else if (code == KeyEvent.VK_F2) {
                ScreensOrder order = ScreensOrder.getInstance();
                order.nextScreen();
            }
        }

        public void keyReleased(KeyEvent e) {
        }
    }
}
